package com.lin.videospringboot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.videospringboot.been.User;
import com.lin.videospringboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {
    @Autowired
    UserMapper userMapper;

    public List<User> queryAll() {
        return userMapper.selectList(null);
    }

    public int addUser(User user) {
        return userMapper.insert(user);
    }

    public User queryUserById(int id) {
        return userMapper.selectById(id);
    }

    public int deleteUserById(int id) {
        return userMapper.deleteById(id);
    }

    public List<User> queryByName(String name) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name",name); //这样可以进行模糊查询--如果要准确查询可以将like改为eq
        return userMapper.selectList(userQueryWrapper);
    }

    public int updateUserById(User user) {
        return userMapper.updateById(user);
    }

    public int countUserName(String name,int userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.ne("userId",userId);
        return userMapper.selectList(queryWrapper).size();
    }

    public int updateUserURL(String url,int userId) {
        User user = userMapper.selectById(userId);
        user.setUrl(url);
        return userMapper.updateById(user);
    }
}
